#include "common.h"
#include "world.h"
#include "tile.h"
#include "magicItem.h"
#include "Creature.h"

void MagicItemHandler::addMagicItem(MagicItemBase *mit)
{
        m_magicItems.push_back(mit);
}
/*
void MagicItemHandler::removeMagicItem(MagicItemBase *mit)
{
        m_magicItems.remove(mit);
}
*/
void MagicItemHandler::OnTimer(int tick)
{
        std::for_each(m_magicItems.begin(), m_magicItems.end(), std::bind2nd(std::mem_fun(&MagicItemBase::OnTimer), tick));
        MAGIC_ITEM_LIST::iterator it;
        for (it = m_magicItems.begin(); it != m_magicItems.end(); ++it) {
                if ((*it)->isInvalid()) {
                        delete *it;
                        m_magicItems.erase(it++);
                }
        }
}

void MagicItemHandler::Draw(BITMAP *surface)
{
        std::for_each(m_magicItems.begin(), m_magicItems.end(), std::bind2nd(std::mem_fun(&MagicItemBase::OnDraw), surface));
}

MagicItemHandler::~MagicItemHandler()
{
        std::for_each(m_magicItems.begin(), m_magicItems.end(), Deleter<MagicItemBase>());
}

/*
FireBolt::FireBolt(int lifetime, int damage, int id, int offset) : MagicItemBase(), m_lifetime(lifetime), m_damage(damage)
{
        m_frame = 1;
        m_lastMoveTime = 0;
        m_tile = INST(TileManager).getTile(3);
        m_monsterid = id;
        m_monsteroffset = offset;
        setSpeed(10, 10);
}

void FireBolt::OnTimer(int tick)
{
        if (m_lastMoveTime + m_frame > tick)
                return;

        if (m_lastMoveTime == 0)
                m_lastMoveTime = tick;

        m_lifetime -= tick - m_lastMoveTime;
        if (m_lifetime < 0) {
                setValid(false);
                return;
        }

        Monster *monster = NULL;
        if (monster == NULL) {
                setValid(false);
                return;
        }

        m_lastMoveTime = tick;
        Rect pos = monster->getRect();

        short dx = pos.x - m_mv.m_x;
        short dy = pos.y - m_mv.m_y;

        if (dx > 0) {
                if (dx > m_mv.m_sx)
                        m_mv.m_x += m_mv.m_sx;
                else
                        m_mv.m_x = pos.x;
        }

        if (dx < 0) {
                if (dx < -m_mv.m_sx)
                        m_mv.m_x -= m_mv.m_sx;
                else
                        m_mv.m_x = pos.x;
        }


        if (dy > 0) {
                if (dy > m_mv.m_sy)
                        m_mv.m_y += m_mv.m_sy;
                else
                        m_mv.m_y = pos.y;
        }

        if (dy < 0) {
                if (dy < -m_mv.m_sy)
                        m_mv.m_y -= m_mv.m_sy;
                else
                        m_mv.m_y = pos.y;
        }

        if (m_mv.m_x == pos.x && m_mv.m_y == pos.y) {
                monster->getLive().changeHp(-m_damage);
                setValid(false);
                return;
        }
//        m_mv.move();
}

void FireBolt::OnDraw(BITMAP *surface)
{
        short sx, sy;
        mapxy_to_screenxy(m_mv.m_x, m_mv.m_y, sx, sy);
        if (sx > 0 && sx < SCREEN_W && sy > 0 && sy < SCREEN_H) {
                draw_sprite(surface, m_tile, sx, sy);
        }
}

FireBolt::~FireBolt()
{
//        allegro_message("Destory FireBall");
}
*/
